<?php

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class RoomRocket extends Base
{
    public $table = 'room_rocket';
    public $tableLog = 'room_rocket_log';
    public $tableLevel = 'room_rocket_level';
    public $tableLevelRes = 'room_rocket_level_res';
    const LEVEL_MAX = 5;

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }
    /*****************************room_rocket_level****************************/

    // 获取某个分值现在的等级
    public function getLevelByVal (int $intimate) {
        $table = $this->tablePrefix . $this->tableLevel;
        $fields = '*';
        $sql = 'SELECT ' . $fields . ' FROM ' . $table . ' WHERE val <= ? AND (val_max >= ? OR val_max =0)';
        $bindings = array($intimate, $intimate);
        $re = DB::select($sql, $bindings);
        if (!$re) {
            return null;
        }

        return self::objToArray($re[0]);
    }

    /**
     * 获取等级
     * @param $level
     * @return mixed
     */
    public function getLevelOne($level)
    {
        $info = DB::table($this -> tableLevel) -> where('level', $level) -> first();

        return self::objToArray($info);
    }

    public function getLevel()
    {
        $info = DB::table($this -> tableLevel) -> orderBy('level', 'asc') -> get();
        return self::objToArray($info);
    }

    /*
     * 获取等级资源
     */
    public function getLevelResBylevel($level)
    {
        $list = DB::table($this -> tableLevelRes) -> where('level', $level) -> orderBy('index', 'asc') -> get();
        $list = self::objToArray($list);
        $data = [];
        if ($list){
            foreach ($list as $key => $value){

                if ($value && $value['content']){
                    $content = json_decode($value['content'], true);
                    if ($content && $content['rs_val']){
                        $content['rs_val_url'] = build_url($content['rs_val']);
                    }
                    $data[$value['index']] = $content;
                }
            }
        }

        return $data;
    }


    /*****************************room_rocket_level****************************/





    /*****************************room_rocket****************************/

    public function addRocket($data)
    {
        return DB::table($this->table)->insertGetId($data);
    }


    /**
     * 火箭加注
     * @param $ID  rocket_id 火箭ID
     * @param int $change_val 加注火箭值
     * @param int $level 火箭等级
     * @return int
     */
    public function incRocketVal($ID, $rocket_val=0, $isExtra=0)
    {

            $obj = DB::table($this->table)
            ->where('rocket_id', $ID);
        $res = $obj->increment('rocket_val', $rocket_val);
        if ($isExtra==0){
            $obj->increment('extra_rocket_val', $rocket_val);
        }
        return $res;
    }

    /**
     * 火箭值 溢出值记录，并结束火箭
     * @param $ID  rocket_id 火箭ID
     * @param int $change_val 加注火箭值
     * @param int $level 火箭等级
     * @return int
     */
    public function incRocketFlowVal($ID, $rocket_flow_val=0)
    {
        $res = DB::table($this->table)
            ->where('rocket_id', $ID)
            ->increment('rocket_flow_val', $rocket_flow_val, ['rocket_flow_state'=>1, 'rocket_flow_time'=>time()]);

        return $res;
    }

    /**
     * 火箭溢出部分 叠加到新火箭 火箭完全释放
     * @param $ID  rocket_id 火箭ID
     * @param int $change_val 加注火箭值
     * @param int $level 火箭等级
     * @return int
     */
    public function incRocketRelease2($ID)
    {
        $res = DB::table($this->table)
            ->where('rocket_id', $ID)
            ->update(['rocket_flow_state'=>2]);

        return $res;
    }

    /**
     * 火箭升级
     * @param $ID
     * @param int $level
     * @return int
     */
    public function incRocketLevel($ID, int $level, $initEndTime)
    {
        $res = DB::table($this->table)
            ->where('rocket_id', $ID)
            ->update(['level'=>$level, 'end_time'=>$initEndTime, 'is_extra'=>0, 'extra_rocket_val'=>0]);
        return $res;
    }

    /**
     * 火箭加时
     */
    public function incRocketExtra($ID, $times)
    {
        $res = DB::table($this->table)
            ->where('rocket_id', $ID)
            ->increment('end_time', $times, ['is_extra'=>1]);
        return $res;
    }


    /**
     * 获取最近一次火箭
     * @param $RID
     * @param $UID
     * @return mixed
     */
    public function getRoomLastRocketByUserId($RID, $UID)
    {
        $info = DB::table($this->table)
            ->where('room_id', $RID)
            ->where('user_id', $UID)
            ->orderBy('rocket_id', 'desc')
            ->first();
        return self::objToArray($info);
    }

    /**
     * 获取当前 有效火箭
     * @param $RID
     * @param $UID
     * @return mixed
     */
    public function getRoomRocketByUserId($RID, $UID)
    {
        $info = DB::table($this->table)
            ->where('room_id', $RID)
            ->where('user_id', $UID)
            ->where('rocket_flow_state', 0)
            ->where('end_time', '>=', time())
            ->where('start_time', '<', time())
            ->orderBy('rocket_id', 'desc')
            ->first();
        return self::objToArray($info);
    }

    /**
     * 获取当前 房间有效火箭
     * @param $RID
     * @param $UID
     * @return mixed
     */
    public function getRoomRocket($RID)
    {
        $info = DB::table($this->table)
            ->where('room_id', $RID)
            ->where('rocket_flow_state', 0)
            ->where('end_time', '>=', time())
            ->where('start_time', '<', time())
            ->groupBy('user_id')
            ->orderBy('rocket_id', 'desc')
            ->get();

        return self::objToArray($info);
    }

    public function findRoomRocket($rocketId)
    {
        $info = DB::table($this->table)
            ->where('rocket_id', $rocketId)
            ->first();

        return self::objToArray($info);
    }
    /*****************************room_rocket****************************/





    /*****************************room_rocket_log****************************/
    public function addLog($insert)
    {
        $insert['add_time'] = time();
        return DB::table($this->tableLog)->insert($insert);

    }

    /*****************************room_rocket_log****************************/




}
